<?php
	class lib
	{
		private $db = array(
							"connected" => false
							);
		function __construct()
		{
			Global $mySql;
			$this->mySql = $mySql;
		}
	public function echoTrueValue($value)
	{
		if(is_null($value))
		{
			echo "Null";
			return;
		}
		if(is_string($value))
		{
			echo $value."<br>";
		}
		elseif(is_bool($value))
		{
			if($value == true)
			{
				echo "true <Br>";
			}
			else
			{
				echo "false <Br>";
			}
		}
		elseif(is_array($value))
		{
			echo "<pre>";
			print_r($value);
			echo "</pre>";
		}
	}
		/** 
	* Set ID fields in order of inbound presedence 
	* 
	* @param string $field 
	*/ 
	public function returnField($field) 
	{ 
		// Form Data First 
		if (FALSE != @$_POST["form"][$field]) 
		{ 
			return $_POST["form"][$field]; 
		} 
		// General Post data next 
		elseif (FALSE != @$_POST[$field]) 
		{ 
			return $_POST[$field]; 
		} 
		// Get Data last 
		elseif (FALSE != @$_GET[$field]) 
		{ 
			return $_GET[$field]; 
		} 
	}
		public function importSettings($data)
		{
			if(!EMPTY($data["redirect"]))
			{
				$this->Settings["redirect"] = $data["redirect"];
			}
		}
		public function registerError($data)
		{
			$this->connectDB();
			$sql = "INSERT INTO error (userid, ipaddress, script, message, refferer) VALUES ('%s', '%s', '%s', '%s', '%s')";
			$query = sprintf($sql,
	            mysql_real_escape_string($data[userid]),
	            mysql_real_escape_string($_SERVER["REMOTE_ADDR"]),
				mysql_real_escape_string($this->getCurrentURL()),
				mysql_real_escape_string($data[message]),
				mysql_real_escape_string($_SERVER["HTTP_REFERER"]));
			$this->mySql->makeQuery($query);
		}
		public function cleanArray($validfields, $array)
		{
			
			foreach($array as $field => $trash)
			{
				if(EMPTY($validfields[$field]))
				{
					unset($array[$field]);
				}
			}
			return $array;
		}
		public function returnTime($mysqltime)
		{
			$timea = getdate(strtotime($mysqltime));
			$time = $timea[0];
			$timesince = time() - $time;
			if($timesince < 30) // Less than 60 seconds ago
			{
				return $timesince . " seconds ago";
			}
			elseif(($timesince > 60) && ($timesince < 3600)) // Less than an hour ago
			{
				$a = $timesince / 60;
				$b = substr($a, 0, strpos($a,"."));
				return $b . " minutes ago.";
			}
			elseif(($timesince > 3600) && ($timesince < 86400)) // Less than a Day Ago
			{
				$a = $timesince / 3600;
				$b = substr($a, 0, strpos($a,"."));
				return $b . " hours ago.";
			}
			elseif(($timesince > 86400) && ($timesince < 259200))
			{
				$a = $timesince / 86400;
				$b = substr($a, 0, strpos($a,"."));
				return $b . " days ago.";
			}
			else
			{
				return date("l M j Y", $time);
			}
		}
		public function redirect($url)
		{
				if (!headers_sent()) 
				{
					header('Location: ' . $url);
					exit;
				}
				else 
				{
					echo "Cannot redirect, for now please click this <a href=".$url.">link</a> instead\n";
					exit;
				}
		}
		public function connectDB()
		{
			/*
				Local Host Connect DB
				$this->lib->connectDB();
			*/
			Global $mySql;
			$this->mySql = $mySql;
			if($this->db['connected'] != 1)
			{
				$this->mySql->connectDB();
				$this->db['connected'] = 1;
			}
		}
		public function getCurrentURL()
		{
			Global $_SERVER;
			
			$php_request_uri = htmlentities(substr($_SERVER['REQUEST_URI'], 0, strcspn($_SERVER['REQUEST_URI'], "\n\r")), ENT_QUOTES);
			if(isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == "on"))
			{
				$protocol = "https://";
			}
			else
			{
				$protocol = "http://";
			}
			$host = $_SERVER['HTTP_HOST'];
			if($_SERVER['HTTP_PORT'] != "" && (($protocol == "http://" && $_SERVER['HTTP_PORT'] =! "80") || ($protocol == "https//" && $_SERVER['HTTP_PORT'] =! "443")))
			{
				$port = ":" . $_SERVER['HTTP_PORT'];
			}
			else
			{
				$port = "";
			}
			return $protocol . $host . $port . $php_request_uri;
		}
	function confirmCreation()
	{
		?>
		<div id=firstbox>
		<form method=POST>
			<input type=hidden value='<?php echo $_POST[form][username] ?>'name="reg[username]">
			<input type=hidden value='<?php echo $_POST[form][password] ?>'name="reg[password]">
			<input type=hidden value='<?php echo $_POST[form][email] ?>' name="reg[email]">
			<input type=hidden value='<?php echo $_POST[form][beta] ?>' name="reg[beta]">
			<b> Please Confirm the Creation of this Account </b>
			<Br>
			Name : <?php echo $_POST[form][username] ?>
			<br>
			Email : <?php echo $_POST[form][email] ?>
			<br>
			<input type=submit value=Go!>
		</form>
		</div>
			
		<?php
	}
	public function getCookies()
	{
		foreach($_COOKIE as $name => $value)
		{
			$temp = explode("#", $value);
			if(FALSE != is_array($temp))
			{
				foreach($temp as $key => $value)
				{
					$data[$name][$key] = $value;
				}
			}
			else
			{
				$data[$name] = $value;
			}
		}
		return $data;
	}
	public function checkEmail($email) 
	{
	   if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) 
	   {
	      return FALSE;
	   }
	   else
	   {
			return true;
	   }
	  }
}
	
?>